package com.example.daemon;

/**
 * @author:CrazyShaQiuShi
 * @email:3105334046@qq.com
 * @descript: 守护线程
 * @version:1.0.0
 */
public class DaemonDemo {
    public static class DaemonT extends Thread{
        @Override
        public void run() {
            while (true){
                System.out.println("I am alive");
                try{
                    Thread.sleep(1000);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[]args) throws InterruptedException {
        Thread thread=new DaemonT();
        /**
         * 设置守护进程，注意此处的进程请务必保证在线程start之前，否则将会抛出异常
         */
        thread.setDaemon(true);
        thread.start();

        /**
         * 注意当用户线程，即主线程退出时，守护线程自动退出
         */
        Thread.sleep(10000);
    }
}
